if(!window['chatty_loaded']) {
	window['chatty_loaded'] = true;

	
	/* begin site data */
	SiteData = {
		init:function() {
			if($.browser.msie) {
	        	this.ie_init();
			}else if($.browser.mozilla||(isChrome=navigator.userAgent.indexOf("Chrome/")>0)){
				this.mozilla_init();
			}
		},
		mozilla_init:function() {},
		ie_init:function() {
			document.documentElement.addBehavior("#default#userdata");
		},
		set_entry:function(key,value,timeout) {
			if($.browser.msie) {
				this.ie_set_entry(key,value,timeout);
			}else if($.browser.mozilla||(isChrome=navigator.userAgent.indexOf("Chrome/")>0)){
				this.mozilla_set_entry(key, value, timeout);
			}
		},
		ie_set_entry:function(key,value,timeout) {
			var ex;
			var oTimeNow = new Date();
			oTimeNow.setSeconds(oTimeNow.getSeconds() + parseInt(timeout));

			with(document.documentElement)try {
				load(key);
				setAttribute("value", value);
				expires=oTimeNow.toUTCString();
				save(key);
				return getAttribute("value");
			} catch (ex){alert("ie_set_entry:" + ex.message)}
		},
		mozilla_set_entry:function(key,value,timeout) {
			var oTimeNow = new Date();
			oTimeNow.setSeconds(oTimeNow.getSeconds() + parseInt(timeout));
			$.cookie(key,value,{expires: oTimeNow});
		},
		get_value:function(key) {
			if($.browser.msie) {
				return this.ie_get_value(key);
			}else if($.browser.mozilla||(isChrome=navigator.userAgent.indexOf("Chrome/")>0)){
				return this.mozilla_get_value(key);
			}
		},
		ie_get_value:function(key) {
			var ex;
			with(document.documentElement)try {
				load(key);
				return getAttribute("value");
			} catch (ex){
				alert(ex.message);
				return null;
			}
		},
		mozilla_get_value:function(key) {
			return $.cookie(key);
		},
		del_entry:function(key) {
			if($.browser.msie) {
				this.ie_del_entry(key);
			}else if($.browser.mozilla||(isChrome=navigator.userAgent.indexOf("Chrome/")>0)){
				this.mozilla_del_entry(key);
			}
		},
		ie_del_entry:function(key) {
			var ex; 
			with(document.documentElement)try{
				load(key);
				expires = new Date(315532799000).toUTCString();
				save(key);
			} catch (ex){alert(ex.message);}
		},
		mozilla_del_entry:function(key) {
			$.cookie(key,null);
		},
		set_expire:function(key, value, timeout) {
			if($.browser.msie) {
				this.ie_set_expire(key,timeout);
			}else if($.browser.mozilla||(isChrome=navigator.userAgent.indexOf("Chrome/")>0)){
				this.mozilla_set_expire(key, value, timeout);
			}
		},
		ie_set_expire:function(key,timeout) {
			var ex;
			var oTimeNow = new Date();
			oTimeNow.setSeconds(oTimeNow.getSeconds() + parseInt(timeout));

			with(document.documentElement)try {
				load(key);
				expires=oTimeNow.toUTCString();
				save(key);
				return getAttribute("value");
			} catch (ex){alert("ie_set_entry:" + ex.message)}
		},
		mozilla_set_expire:function(key,value,timeout) {
			var oTimeNow = new Date();
			oTimeNow.setSeconds(oTimeNow.getSeconds() + parseInt(timeout));
			$.cookie(key,value,{expires: oTimeNow});
		}
	};
	SiteData.init();
	/* end site data */


	
	
	function log(msg) {
		/*var c = document.getElementById('debug');
		var d = document.createElement('div');
		d.innerHTML = msg;
		c.appendChild(d);*/
	}
	
	var chatty_001_ref;
	Chatty = function(config) {
		this.config = {
				title:'管理员邀请您进行沟通!',
				imProxy: 'http://127.0.0.1:8080',
				needConfirm:false
		};
		this.setConfig(config);
		chatty_001_ref = this;

		$(document).ready(function() {
			chatty_001_ref.init();
		});
	};
	
	Chatty.prototype = {
		setConfig:function(cfg){
			if(!cfg){return}
			for(var i in cfg){
				this.config[i]=cfg[i];
			}
		},
		init : function() {
			//start detect msg events
			this.initSession();
		},
		initSession:function() {
			this.win_token = Math.random();
			var url = this.config.imProxy + "/newses?uid="+this.config.uid+"&callback=?";
			$.getJSON(url,
				function(data) {
					chatty_001_ref.session = data.session;
					chatty_001_ref.detectPoll();
					
					/*if(data.status==0) {
						alert("session not matched");
					} else if(data.status == 1) {
						alert(3);
					}*/
				}
			);
		},
		detectPoll: function() {
			//client side detection: if have cookie, then continue detect until other window closed
			var win_token = SiteData.get_value('chatty');
			log('detectPoll: '+win_token);
			if(win_token !== null && win_token.length != 0) {
				//if owner, then refresh session
				if(chatty_001_ref.win_token == win_token) {
					SiteData.set_expire('chatty', chatty_001_ref.win_token, 60);
				}
				window.setTimeout(function() {
					chatty_001_ref.detectPoll();
				}, 5000);
				return;
			}

			//server side detection: polling server every 5s.
			//  if have messages for this uid/session, then return a msg with status=1
			var url = this.config.imProxy + "/detect?uid="+this.config.uid+"&session=" + chatty_001_ref.session + "&callback=?";
			$.getJSON(url,
				function(data) {						
					if(data.status==0) {
						//空闲
						window.setTimeout(function() {
							chatty_001_ref.detectPoll();
						}, 5000);
					} else if(data.status == 1) {
						var win_token = SiteData.get_value("chatty");
						log('poll callback: '+win_token);
						//client check again to avoid any precise confictor arrrived here
						//only session is null/empty that can show window
						if (win_token === null || win_token.length === 0) {
							chatty_001_ref.mf_showDialog(data);
						}

						window.setTimeout(function() {
							chatty_001_ref.detectPoll();
						}, 5000);
					} else {//XXX session not matched
						window.setTimeout(function() {
							chatty_001_ref.detectPoll();
						}, 5000);
					}
				}
			);
		},
		mf_showDialog : function(data) {
			if(!this.config.needConfirm || confirm(this.config.title)) {
				SiteData.set_entry('chatty', chatty_001_ref.win_token,60);
				try{
					var url = this.config.imProxy + "/client?sender=" + data.sender + "&session="+ chatty_001_ref.session + "&uid=" + this.config.uid;
					var s = '<html><body style="margin:0; padding:0;margin-top:0px;"><iframe src="'+url+'" width="100%"  height="100%" frameborder="0"></iframe>';
					s += '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></'+'script>';
					s += '<script type="text/javascript" src="http://127.0.0.1:8080/js/jquery.cookie.js"></'+'script>';		
					s += '<script type="text/javascript" src="http://127.0.0.1:8080/js/sitedata.js"></'+'script>';
					s += '<script>';
					s += 'var stop=false;';
					s += 'SiteData.set_expire("chatty",' + chatty_001_ref.win_token + ',60);'
					s += 'setInterval(function(){if(!stop)SiteData.set_expire("chatty",' + chatty_001_ref.win_token + ',60)},10000);'
					s += 'function pageUnload(){stop=true;SiteData.del_entry("chatty");}';
					s += 'if (window.attachEvent) {';
					s += '  window.attachEvent("onunload", pageUnload);';
					s += '} else {';
					s += '	window.addEventListener("unload", pageUnload, false);';
					s += '}';
					s += '</'+'script></body></html>';
					
					var target = 'about:blank';
					var newwindow = window.open(target,'chatty_client','height=420,width=600,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
					if(newwindow) {
						newwindow.focus();
						newwindow.document.write(s);
						newwindow.document.close();	
					}
				}catch(e){alert("e:" + e)}
			}
		}
	};
}